package com.smrs.report.webapp.action;

import java.io.IOException;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;

import com.jof.framework.excel.ExcelExportTemplate;
import com.jof.framework.util.Pager;
import com.jof.framework.util.Servlets;
import com.smrs.report.excel.MonthlyDeliverReportTemplate;
import com.smrs.report.model.ReportMonthlyDeliverModel;
import com.smrs.report.model.ReportSearchModel;




public class MonthlyDeliverOrderJsonAction extends ReportBaseJsonAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Pager<ReportMonthlyDeliverModel> pager = new Pager<ReportMonthlyDeliverModel>();
	public Pager<ReportMonthlyDeliverModel> getPager() {
		return pager;
	}

	public void setPager(Pager<ReportMonthlyDeliverModel> pager) {
		this.pager = pager;
	}


	@Override
	public String execute() throws Exception {
		// 去空格
		getPager().setCurrentPage(page);
		getPager().setPageSize(rows);
		if(StringUtils.isNotEmpty(sidx)) {
			getPager().setOrderProperty(sidx);
			getPager().setOrder(StringUtils.defaultIfEmpty(sord, "asc"));
		}
		if(searchModel!=null){
			String startYearMonth = searchModel.getStartYear()+"-"+searchModel.getStartMonth();
			Date startDate = DateUtils.parseDate(startYearMonth, "yyyy-MM");
			String endYearMonth = searchModel.getEndYear()+"-"+searchModel.getEndMonth();
			Date endDate = DateUtils.parseDate(endYearMonth, "yyyy-MM");
			searchModel.setStartDate(startDate);
			searchModel.setEndDate(endDate);
		}else{
			searchModel =new ReportSearchModel();
		}
		pager = reportOfDeliver.getDeliverReportOfMonthly(searchModel, pager);
		this.setTotal(this.getPager().getTotalPages());
		this.setRecords(this.getPager().getTotalRecords());
		return SUCCESS;
	}
	
	public String toReportQueryPage(){
		preparePageList();
		return SUCCESS;
	}
	
	public String exportMonthlyDeliver(){
		getResponse().setContentType(Servlets.EXCEL_TYPE);
		Servlets.setFileDownloadHeader(getRequest(),getResponse(), "月销售报表.xlsx");
		ExcelExportTemplate<ReportSearchModel> exportUserListTemplate = new MonthlyDeliverReportTemplate();
		//ReportMonthlyDeliverModel reportModel = new ReportMonthlyDeliverModel();
		try {
			exportUserListTemplate.doExport(getResponse().getOutputStream(), searchModel);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
	}
}
